package com.kgc.ebookk.ebookkmanger.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.ebookk.ebookkmanger.mapper.EbookCategoryMapper;
import com.kgc.ebookk.ebookkmanger.mapper.EbookEntryMapper;
import com.kgc.ebookk.ebookkmanger.mapper.ViewEntryCategoryMapper;
import com.kgc.ebookk.ebookkmanger.pojo.EbookCategory;
import com.kgc.ebookk.ebookkmanger.pojo.EbookEntry;
import com.kgc.ebookk.ebookkmanger.pojo.ViewEntryCategory;
import com.kgc.ebookk.ebookkmanger.pojo.ViewEntryCategoryExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EbookServiceImpl implements EbookService {
    @Resource
    EbookCategoryMapper categoryMapper;
    @Resource
    EbookEntryMapper entryMapper;
    @Resource
    ViewEntryCategoryMapper viewEntryCategoryMapper;

    @Override
    public List<EbookCategory> queryAll() {
        return categoryMapper.selectByExample(null);
    }

    @Override
    public PageInfo<ViewEntryCategory> querybookAll(Integer categoryid, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("createdate desc");
        ViewEntryCategoryExample example=new ViewEntryCategoryExample();
        ViewEntryCategoryExample.Criteria criteria=example.createCriteria();

        if (categoryid!=0){
            criteria.andCategoryidEqualTo(categoryid);
            List<ViewEntryCategory> list=viewEntryCategoryMapper.selectByExample(example);
            PageInfo<ViewEntryCategory> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }else{
            List<ViewEntryCategory> list=viewEntryCategoryMapper.selectByExample(null);
            PageInfo<ViewEntryCategory> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }

    }

    @Override
    public EbookEntry queryByid(Integer id) {
        return entryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateEbook(EbookEntry ebookEntry) {
        return entryMapper.updateByPrimaryKeySelective(ebookEntry);
    }

    @Override
    public int addEbook(EbookEntry ebookEntry) {
        return entryMapper.insertSelective(ebookEntry);
    }

    @Override
    public int delEbook(Integer id) {
        return entryMapper.deleteByPrimaryKey(id);
    }
}
